<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/27
 * Time: 2:23 PM
 */

namespace App\BO\OrderStatus;

class OrderStateDefinition
{
    // order state 字段值
    public const STATE_DEFAULT_VALUE = 0;  // 为了兼容之前的判断条件

    // 订单等待初始化
    public const STATE_BEFORE_INIT = 1;  // 为了兼容之前的判断条件

    // 订单待支付
    public const STATE_WAITING_PAY = 2;  // 订单待支付，不可派单
    public const STATE_WAITING_PAY_COULD_DISPATCH = 3;  // 订单待支付，可源单，后台添加，后期统一结算

    // 支付相关状态
    public const STATE_PAY_SUCCESS = 11;  // 支付成功
    public const STATE_PAY_FAILED = 12;  // 支付失败

    // 退款相关状态
    public const STATE_REFUND_WAITING_CHECK = 21;  // 退款申请待审核
    public const STATE_REFUNDING = 22;  // 退款中
    public const STATE_REFUND_SUCCESS = 23;  // 退款成功
    public const STATE_REFUND_WAITING_THIRD_PART_NOTIFY = 24;  // 等待第三方回调通知退款是否成功
    public const STATE_REFUND_FAILED = 25;  // 退款失败
    public const STATE_REFUND_REJECT_BY_ADMIN = 26;  // 退款被管理员拒绝
    public const STATE_REFUND_CANCELLED_BY_USER = 27;  // 退款被用户取消

    // 订单分配相关状态
    public const STATE_WAITING_DISTRIBUTION = 41; // 订单待分配

    // 订单服务相关状态
    public const STATE_WAITING_SERVICE = 51; // 订单待服务
    public const STATE_REPORTED = 52; // 订单已经报备
    public const STATE_SERVICING = 53; // 订单服务中

    // 订单平价相关状态
    public const STATE_WAITING_COMMENT = 61; // 订单待评价
    public const STATE_REVIEWED = 62; // 订单已经评价
    public const STATE_NOT_PAID_NAD_FINISHED = 63; // 末支付，已经完成订单

    public const STATE_EXPIRED = 91; // 订单过期末支付
    public const STATE_USER_CANCEL = 92;  // 用户手动取消定单

    public const STATES = [
        [
            'state' => self::STATE_WAITING_PAY,
            'des' => '待支付',
        ],
        [
            'state' => self::STATE_WAITING_PAY_COULD_DISPATCH,
            'des' => '待支付，可派单',
        ],
        [
            'state' => self::STATE_PAY_SUCCESS,
            'des' => '支付成功',
        ],
        [
            'state' => self::STATE_REFUND_WAITING_CHECK,
            'des' => '退款待审核',
        ],
        [
            'state' => self::STATE_REFUND_SUCCESS,
            'des' => '已经退单',
        ],
        [
            'state' => self::STATE_REFUND_REJECT_BY_ADMIN,
            'des' => '退款被驳回',
        ],
        [
            'state' => self::STATE_WAITING_DISTRIBUTION,
            'des' => '待分配',
        ],
        [
            'state' => self::STATE_WAITING_SERVICE,
            'des' => '待服务',
        ],
        [
            'state' => self::STATE_REPORTED,
            'des' => '已报备',
        ],
        [
            'state' => self::STATE_WAITING_COMMENT,
            'des' => '待评价',
        ],
        [
            'state' => self::STATE_REVIEWED,
            'des' => '已经评价',
        ],
        [
            'state' => self::STATE_EXPIRED,
            'des' => '过期末支付',
        ],
        [
            'state' => self::STATE_USER_CANCEL,
            'des' => '用户主动取消',
        ],
    ];

    /**
     * @var array
     */
    private static $map = [];

    /**
     * @return void
     */
    private static function genMap()
    {
        $map = array_combine(
            array_column(self::STATES, 'state'),
            array_column(self::STATES, 'des')
        );

        self::$map = $map;
    }

    /**
     * @param int $state
     * @return string
     */
    public static function getDescByState(int $state)
    {
        if (empty(self::$map)) {
            self::genMap();
        }
        return self::$map[$state] ?? '';
    }
}
